<template>
    <div class="cmt-container">
        <h3>添加评论</h3>
        <hr>
        <textarea placeholder="输入评论内容（最多120字）" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="addComment">添加评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comments">
                <div class="cmt-title">
                    第{{ i + 1 }}楼&nbsp;&nbsp;用户:{{ item.user_name }}&nbsp;&nbsp;发表时间：{{ item.add_time | dateFormat }}
                </div>
                <div class="cmt-body">
                    {{ item.content.length === 0 ? '此用户很懒，嘛都没说': item.content }}
                </div>
            </div>
            <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>

        </div>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui'

    export default {
        name: "Comment",
        data() {
            return {
                pageindex: 1,
                comments: [],
                msg:''
            }
        },
        created() {
            this.getComment()
        },
        methods: {
            getComment() {
                this.$http.get('http://www.liulongbin.top:3005/api/getcomments/' + this.id + '?pageindex=' + this.pageindex).then(result => {
                    if (result.body.status === 0) {
                        this.comments = this.comments.concat(result.body.message)
                    } else {
                        Toast('评论加载失败')
                    }
                })
            },
            getMore() {
                this.pageindex++
                this.getComment()
            },
            addComment(){
                if(this.msg.trim().length === 0){
                   return   Toast('请输入您的评论后再提交')
                }

                //  设置了根URL后，其后的URL是做字符串拼接，所以不要以/开头，否则会出现//的情况
                this.$http.post('api/postcomment/' + this.id,{ content: this.msg.trim()}).then(result => {
                    if(result.body.status === 0){
                        const  cmt  = {
                            user_name:'匿名用户',
                            add_time:Date.now(),
                            content:this.msg.trim()
                        }

                        this.comments.unshift(cmt)
                        this.getComment()
                        this.msg=''

                    }else{
                        Toast('添加评论失败')
                    }
                })
            }
        },
        props: ['id']

    }
</script>

<style scoped lang="less">
    .cmt-container {
        h3 {
            font-size: 18px;
        }

        textarea {
            font-size: 16px;
            margin: 0;
        }

        .cmt-list {
            .cmt-item {
                .cmt-title {
                    background-color: #ccc;
                    font-size: 13px;
                    line-height: 30px;
                }

                .cmt-body {
                    font-size: 15px;
                    line-height: 35px;
                }
            }
        }
    }
</style>